home *** CD-ROM | disk | FTP | other *** search
/ Czech Logic, Card & Gambling Games / Logické hry.iso / hry / Robocode / robocode-setup-1.0.7.jar / extract.jar / robots / sample / Target.java < prev    next >
Encoding:
Java Source  |  2005-02-18  |  1.0 KB  |  47 lines

  1. package sample;
  2. import robocode.*;
  3. /**
  4.  * Target - a sample robot by Mathew Nelson
  5.  * 
  6.  * Sits still.  Moves every time energy drops by 20.
  7.  * This Robot demonstrates custom events.
  8.  */
  9. public class Target extends AdvancedRobot {
  10.  
  11.     int trigger;            // Keeps track of when to move
  12.     
  13.     /**
  14.      * TrackFire's run method
  15.      */
  16.     public void run() {
  17.         // Initially, we'll move when life hits 80
  18.         trigger = 80;
  19.         // Add a custom event named "trigger hit",
  20.         addCustomEvent(
  21.             new Condition("triggerhit") { 
  22.                 public boolean test() {
  23.                     return (getEnergy() <= trigger);
  24.                 };
  25.             }
  26.         );
  27.     }
  28.     /**
  29.      * onCustomEvent handler
  30.      */    
  31.     public void onCustomEvent(CustomEvent e)
  32.     {
  33.         // If our custom event "triggerhit" went off,
  34.         if (e.getCondition().getName().equals("triggerhit"))
  35.         {
  36.             // Adjust the trigger value, or
  37.             // else the event will fire again and again and again...
  38.             trigger -= 20;
  39.             out.println("Ouch, down to " + (int)(getEnergy()+.5) + " energy.");
  40.             // move around a bit.
  41.             turnLeft(65);
  42.             ahead(100);
  43.         }
  44.     }
  45. }
  46.  
  47.